import pymysql



class MysqlTool:
    conn = None

    @classmethod
    def __get_conn(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2021",
                                       database="ihrm", charset="utf8")
        return cls.conn

    @classmethod
    def __close_conn(cls):
        if cls.conn:
            cls.conn.close()
            cls.conn = None


    @classmethod
    def get_one(cls, my_sql):
        cur = None
        cf = None
        try:
            cur = cls.__get_conn().cursor()
            cur.execute(my_sql)
            cf = cur.fetchone()
        except Exception as e:
            print(e)
            raise e
        finally:
            cur.close()
            cls.__close_conn()
            return cf

    @classmethod
    def get_db(cls, my_sql):
        cur = None
        res = None
        try:
            cur = cls.__get_conn().cursor()
            cur.execute(my_sql)
            res = cur.rowcount
            cls.__get_conn().commit()
        except Exception as e:
            print(e)
            cls.__get_conn().rollback()
            raise e
        finally:
            cur.close()
            cls.__close_conn()
            return res


if __name__ == "__main__":
    # sql = "select * from bs_user where mobile = 13800000002"
    my_sql = "delete from bs_user where id = '800000004';"
    print("影响的行数:", MysqlTool.get_db(my_sql))
